package ru.averta.notes.domain;

import java.util.*;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import ru.averta.notes.domain.abstr.BusinessObject;
import ru.averta.notes.domain.abstr.UserableObject;
import ru.averta.notes.domain.flags.IHierarchical;

@Entity
@Table(name = "Folders") 
public class Folder extends UserableObject implements BusinessObject, IHierarchical<Folder> {

	public static final String DOMAIN_SUB_FOLDERS = "childs";

	@Column(name = "TITLE")
	private String title;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="PARENT_FOLDER_ID")
	private Folder parent;

	@OneToMany(mappedBy="parent", fetch=FetchType.LAZY)
	private Collection<Folder> childs = new ArrayList<Folder>();

	@OneToMany(mappedBy="folder", fetch=FetchType.LAZY)
	private Collection<Note> notes = new ArrayList<Note>();

	protected void addNote(Note note) {
		if(isAddUserable(note)) {
			note.setFolder0(this);
			notes.add(note);
		}
	}

	protected void removeNote(Note note) {
		if(isAddUserable(note)) {
			note.setFolder0(null);
			notes.remove(note);
		}
	}

	
	private void addChild(Folder object) {
		if(isAddUserable(object)) {
			childs.add(object);
			object.parent = this;
		}
	}
	
	private void removeChild(Folder object) {
		if(isAddUserable(object)) {
			childs.remove(object);
			object.parent = null;
		}
	}

	public void setParent(Folder object) {
		if(isAddUserable(object)) {
			if (parent != null) parent.removeChild(this);
			if (object != null) object.addChild(this);
		}
	}

	/*
	 *  GET / SET
	 */

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setNotes(Collection<Note> notes) {
		this.notes = notes;
	}

	public Collection<Note> getNotes() {
		return notes;
	}

	public Folder getParent() {
		return parent;
	}

	public Collection<Folder> getChilds() {
		return childs;
	}

	

}
